<?php

require_once 'BaseService.php';
require_once 'dao/ProductDao.php';
require_once 'dao/ProductFilterDao.php';
require_once 'IProductService.php';
require_once 'util/ServiceUtil.php';
require_once 'util/StringUtil.php';

class ProductService extends BaseService implements IProductService
{
	public static $NAME = "ProductService";
	
	protected $myProduct;
	/**
	 * 
	 */
	public function ProductService() {
		$this->initializeDB();
		$this->myProduct = new ProductDao();
	}
	
	public function create(ProductDao $object)
	{
		if($object)
		{
			try {
				$this->sql = "INSERT INTO product(description,image,name,pdf,swf,testcode,price,category,mprice) 
							VALUES('$object->description',
							'$object->image',
							'$object->name',
							'$object->pdf',
							'$object->swf',
							'$object->testcode',
							'$object->price',
							'$object->category','$object->mprice')";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
				
			}catch (Exception  $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function read(ProductFilterDao $object)
	{
		$keywords = $object->keywords;
		$min = $object->min;
		$max = $object->max;
		$category = $object->category;
			
		if($keywords==NULL)
		{
			$this->sql  = "SELECT * FROM product ORDER BY price ASC";
			if($min!=-1&&$max!=-1)
			{
				$this->sql = "SELECT * FROM product WHERE price >='$min' AND price<='$max' ORDER BY price ASC";
			}else if($category!=NULL)
			{
				$this->sql = "SELECT * FROM product WHERE category='$category' ORDER BY price ASC";
			}
		}else
		{
			$this->sql = "SELECT * FROM product WHERE name LIKE '%$keywords%' ORDER BY price ASC";
		}
		
		try 
		{
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			
			for($i=0;$i<$this->numRows;$i++)
 			{
 				$row = mysql_fetch_object($this->query);
 				
 				$this->myProduct = new ProductDao();
 				
 				$this->myProduct->id = $row->id;
				$this->myProduct->name = $row->name;
				$this->myProduct->description = $row->description;
				$this->myProduct->image = $row->image;
				$this->myProduct->pdf = $row->pdf;
				$this->myProduct->swf = $row->swf;
				$this->myProduct->price = $row->price;
				$this->myProduct->testcode = $row->testcode;
				$this->myProduct->time = $row->time;
				$this->myProduct->category = $row->category;
				$this->myProduct->mprice = $row->mprice;
				
				$this->result[] = $this->myProduct;
 			}
			
			return $this->result;
		}catch (Exception  $exc)
		{
			return ServiceUtil::getErrorResult(ServiceUtil::ERROR,(__CLASS__).("::").(__FUNCTION__),ServiceUtil::ERROR_CODE_0x000000,$exc);
		}
		return NULL;
	}
	
	public function update($object)
	{
		if($object)
		{
			try {
				$this->sql = "UPDATE TABLE product SET product.name=$object->name,
								product.description=$object->description,
								product.price=$object->price,product.category=$object->category,product.mprice=$object->mprice";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch (Exception  $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public  function delete($id)
	{
		if($id)
		{
			try {
				$this->sql = "DELETE FROM product WHERE id='$id'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch (Exception  $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
}

?>